package
{
	import fl.controls.Button;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;

    [SWF(width="640",height="550")]
	public class TowerWarMap extends Sprite
	{
	
		public static var instance:TowerWarMap;
		
		public var WIDTH:int = 640;
		
		public var HEIGHT:int = 460;
		
		public var background:BackgroundView;
		
		public var gradMap:GradMap;

		public var plant:PlantPanel;
		
		public var mouseTip:MouseTip;
		
		public var tool:ToolPanel;
		
		public var plantView:PlantView;
		
		public static const level:int =20;
	
		public function TowerWarMap()
		{
			instance = this;
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			this.addEventListener(Event.ENTER_FRAME,loop);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
		}
		
		private function mouseUp(e:MouseEvent):void{
			mouseTip.hide();
			mouseTip.cls = null;
			plant.selectPlant = null;
		}
		
		protected function loop(event:Event):void
		{
			mouseTip.move();
		}
		
		public function showMouseTip():void{
			mouseTip.cls = plant.selectPlant;
			this.addChild(mouseTip);
		}
		
		public function lineEdit(e:MouseEvent):void{
			plantView.visible = !plantView.visible;
		}
		
		
		protected function addToStage(event:Event):void
		{
			background = new BackgroundView();
			this.addChild(background);
	        gradMap = new GradMap();
			this.addChild(gradMap);
			plant = new PlantPanel();
			this.addChild(plant);
			mouseTip = new MouseTip(this.stage);
		    this.addChild(mouseTip);
			gradMap.initline();
			plantView = new PlantView();
			this.addChild(plantView);
			tool = new ToolPanel();
			this.addChild(tool);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN,enterKey);
		}
		
		protected function enterKey(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.ENTER){
				var xml:XML = <line>
				            
					          </line>;
				var line:Array = gradMap.line;
				var point:Point;
				while(line.length){
					point = line.shift();
					var xxx:XML = <elements x='' y=''/>;
					xxx.@x = point.x;
					xxx.@y = point.y;
					xml.appendChild(xxx);
				}
				trace(xml.toString());
//				var file:FileReference = new FileReference();
//				file.save(xml,"map.xml");
			}
		
		}
	}
}